using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ScreenManager;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using LabyrinthProject;
using System.Threading;

namespace LabirintGUI.Screens
{
    class NewGameScreen1stPlayer : ScreenManager.Screen
    {
        SpriteBatch MainSpriteBatch;
        Texture2D Background;
        MouseHandler ourMouse; //An instance of the MouseHandler class
        List<Button> buttonlist; //Our list of buttons
        MouseState mouseState; //MouseState needed for what checking 

        Button dotButtonRightHigh; //An instance of Button        
        Button dotButtonRightMedium; //An instance of Button       
        Button dotButtonRightLow; //An instance of Button        
        Button dotButtonLeftHigh; //An instance of Button        
        Button dotButtonLeftMedium; //An instance of Button       
        Button dotButtonLeftLow; //An instance of Button 
        Button leftButton; //An instance of Button 
        Button rightButton; //An instance of Button 

        Video myVideoFile1;
        Video myVideoFile2;
        Video myVideoFile3; // Create the variables needed to hold and play back my video.        
        VideoPlayer videoPlayer;

        GraphicsDevice gDevice;

        public NewGameScreen1stPlayer(GraphicsDevice device)
            : base(device, "new_game_1")
        {
            gDevice = device;
        }

        public override void Shutdown()
        {
            videoPlayer.Stop();
            base.Shutdown();
        }

        public override bool Init(Game game)
        {
            
            videoPlayer = new VideoPlayer();
            myVideoFile1 = game.Content.Load<Video>(@"Robo");
            myVideoFile2 = game.Content.Load<Video>(@"Koko");
            myVideoFile3 = game.Content.Load<Video>(@"Krakow");

            Background = game.Content.Load<Texture2D>(@"new_game_1");

            Texture2D textureLeft = game.Content.Load<Texture2D>(@"left_button");
            Texture2D textureLeftHovered = game.Content.Load<Texture2D>(@"left_button_hovered");
            Texture2D textureRight = game.Content.Load<Texture2D>(@"right_button");
            Texture2D textureRightHovered = game.Content.Load<Texture2D>(@"right_button_hovered");

            Texture2D textureDot = game.Content.Load<Texture2D>(@"dot_button");
            Texture2D textureDotHovered = game.Content.Load<Texture2D>(@"dot_button_hovered");
            Texture2D textureDotClicked = game.Content.Load<Texture2D>(@"dot_button_clicked");

            Rectangle rectangleDotRightHigh = new Rectangle((int)buttonRH.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
                (int)buttonRH.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                 textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightMedium = new Rectangle((int)buttonRM.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRM.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightLow = new Rectangle((int)buttonRL.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRL.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftHigh = new Rectangle((int)buttonLH.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLH.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftMedium = new Rectangle((int)buttonLM.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLM.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftLow = new Rectangle((int)buttonLL.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLL.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleLeft = new Rectangle((int)741 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureLeft.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
              (int)1215 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureLeft.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
               textureLeft.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
              , textureLeft.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleRight = new Rectangle((int)1330 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureRight.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
                (int)1215 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureRight.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureRight.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureRight.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);


            dotButtonRightHigh = new Button(rectangleDotRightHigh, textureDot, textureDotHovered, textureDotClicked, "RH");
            dotButtonRightMedium = new Button(rectangleDotRightMedium, textureDot, textureDotHovered, textureDotClicked, "RM");
            dotButtonRightLow = new Button(rectangleDotRightLow, textureDot, textureDotHovered, textureDotClicked, "RL");
            dotButtonLeftHigh = new Button(rectangleDotLeftHigh, textureDot, textureDotHovered, textureDotClicked, "LH");
            dotButtonLeftMedium = new Button(rectangleDotLeftMedium, textureDot, textureDotHovered, textureDotClicked, "LM");
            dotButtonLeftLow = new Button(rectangleDotLeftLow, textureDot, textureDotHovered, textureDotClicked, "LL");

            leftButton = new Button(rectangleLeft, textureLeft, textureLeftHovered, textureLeftHovered, "left");
            rightButton = new Button(rectangleRight, textureRight, textureRightHovered, textureRightHovered, "right");


            buttonlist = new List<Button>(); //Actually creating the list
            this.buttonlist.Add(dotButtonRightHigh);
            this.buttonlist.Add(dotButtonRightMedium);
            this.buttonlist.Add(dotButtonRightLow);
            this.buttonlist.Add(dotButtonLeftHigh);
            this.buttonlist.Add(dotButtonLeftMedium);
            this.buttonlist.Add(dotButtonLeftLow);
            this.buttonlist.Add(leftButton);
            this.buttonlist.Add(rightButton);

            ourMouse = new MouseHandler(new Vector2(0, 0), game.Content.Load<Texture2D>(@"pointer"));

            videoPlayer.Play(myVideoFile1);
            videoPlayer.IsLooped = true;

            return base.Init(game);
        }


        public override void Draw(GameTime gameTime)
        {

            MainSpriteBatch = new SpriteBatch(gDevice);

            this.MainSpriteBatch.Begin();
            Vector2 position = new Vector2(0, 0);
            Rectangle backgroundRectangle = new Rectangle(0, 0, _device.Viewport.Width, _device.Viewport.Height);
            this.MainSpriteBatch.Draw(Background, backgroundRectangle, Color.White);


            foreach (Button b in buttonlist) //For every button run through the code between the {}
            {
                //For every button in our list, draw it
                b.Draw(MainSpriteBatch);

                if (b.hovering) //If we are hovering
                {
                    //Draw the hover sprite
                    MainSpriteBatch.Draw(b.OnHovering, b.position, Color.White);
                }
                if (b.clicking) //If we are clicking
                {
                    //Draw the clicking sprite.
                    MainSpriteBatch.Draw(b.OnClicking, b.position, Color.White);
                    switch (b.name)
                    {
                        case "LL":
                            SCREEN_MANAGER.goto_screen("main");
                            break;
                        case "RL":
                            //videoPlayer.Stop();
                            //PLAY GAME
                            //_game.Exit();

                            SCREEN_MANAGER.goto_screen("new_game_2");
                            break;
                        case "left":
                            changeChar(false);
                            break;
                        case "right":
                            changeChar(true);
                            break;

                    }

                }
            }

            if (videoPlayer.State == MediaState.Playing)
            {
                MainSpriteBatch.Draw(videoPlayer.GetTexture(), 
                    new Rectangle(708 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
                        700 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight,
                        660 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
                        470 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight), Color.White);
            }

            //Draw the mouse.
            ourMouse.Draw(MainSpriteBatch);
            //Done
            MainSpriteBatch.End();



            base.Draw(gameTime);
        }

        private void changeChar(bool isDirectionRight)
        {
            int oldChar = (int)Properties.character1;
            int newChar;

            //TODO
            if (isDirectionRight)
            {
                newChar = (oldChar + 1) % 3;
            }
            else
            {
                newChar = (oldChar + 2) % 3;
            }

            Properties.character1 = (Character)newChar;
            switch (Properties.character1)
            {
                case Character.Robo:
                    videoPlayer.Play(myVideoFile1);
                    break;
                case Character.Koko:
                    videoPlayer.Play(myVideoFile2);
                    break;
                case Character.Krakow:
                    videoPlayer.Play(myVideoFile3);
                    break;


            }
        }

        private void HandleMouse()
        {
            mouseState = Mouse.GetState(); //Get the current state of the mouse
            foreach (Button b in buttonlist)
            {
                if (ourMouse.ButtonClick(b)) //There's only one button, so we just hardcode it.
                    //If we're hovering over the mouse
                    b.hovering = true; //We ARE hovering
                else
                    b.hovering = false; //Not hovering.
                //We don't even need to use ButtonClick() again if we know if we're hovering.
                if (mouseState.LeftButton == ButtonState.Pressed && b.hovering) //If we're clicking with the Left Mouse Button and we're over the button.
                    b.clicking = true; //We ARE clicking
                else
                    b.clicking = false; //Not clicking
            }
        }


        public override void Update(GameTime gameTime)
        {
            HandleMouse(); //Check clicking
            ourMouse.Update();
            base.Update(gameTime);
        }

    }
}
